/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.siteEncheres.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import java.util.HashMap;
import org.siteEncheres.client.command.Cmd_creer_seance;
import org.siteEncheres.client.command.Cmd_gerer_compte;
import org.siteEncheres.client.command.Cmd_identifier_utilisateur;
import org.siteEncheres.client.command.Cmd_inscrire_uilisateur;
import org.siteEncheres.client.command.Cmd_lister_seances;
import org.siteEncheres.client.command.Cmd_modifier_profil;
import org.siteEncheres.client.command.Command;
import org.siteEncheres.client.utilisateur.IhmIdentifierUtilisateur;

/**
 * Main entry point.
 *
 * @author Francois
 */
public class MainEntryPoint implements EntryPoint, ValueChangeHandler {

    /** 
     * Creates a new instance of MainEntryPoint
     */
    
    HashMap<String, Command> listeCommande = listeCommande = new HashMap<String, Command>();
    HorizontalPanel panelHautDroite = new HorizontalPanel();
    SimplePanel panelCentre = new SimplePanel();
    IhmConnexion ihmConnexion;
    
    public MainEntryPoint(){
    }

    /** 
     * The entry point method, called automatically by loading a module
     * that declares an implementing class as an entry-point
     */
    public void onModuleLoad() {
        VerticalPanel panelRoot = new VerticalPanel();
        HorizontalPanel panelHaut = new HorizontalPanel();
        HorizontalPanel panelBas = new HorizontalPanel();
        HorizontalPanel panelVideHautMilieu = new HorizontalPanel();
        HorizontalPanel panelVideHautGauche = new HorizontalPanel();
        SimplePanel panelIdentification = new SimplePanel();
        
        
        panelRoot.setWidth("100%");
        panelRoot.setHeight("100%");
        panelHaut.setWidth("100%");
        
        IhmChercherCateg ihmChercherCatego = new IhmChercherCateg();
        IhmIdentifierUtilisateur ihmIdentifierUtilisateur = new IhmIdentifierUtilisateur();
        //IhmUtilisateurConnecte ihmUtilisateurConnecte = new IhmUtilisateurConnecte();
        IhmActionMenu ihmActionMenu = new IhmActionMenu();
        ihmConnexion = new IhmConnexion();
        panelIdentification.add(ihmConnexion);
        
        panelHaut.add(panelVideHautGauche);
        panelHaut.setCellWidth(panelVideHautGauche, "30%");
        panelHaut.add(ihmChercherCatego);
        panelHaut.setCellVerticalAlignment(ihmChercherCatego, HasVerticalAlignment.ALIGN_BOTTOM);
        panelHaut.setCellWidth(ihmChercherCatego, "30%");
        panelHaut.add(panelVideHautMilieu);
        panelHaut.setCellWidth(panelVideHautMilieu, "20%");
        panelHaut.add(panelIdentification);
        panelHaut.setCellWidth(panelIdentification, "20%");
        
        //panelCentre.add(new IhmListerSeance());
        
        panelBas.add(ihmActionMenu);
        panelBas.setCellWidth(ihmActionMenu, "15%");
        panelBas.add(panelCentre);
        panelBas.setCellWidth(panelCentre, "85%");
        
        panelRoot.add(panelHaut);
        panelRoot.setCellHeight(panelHaut,"10%");
        panelRoot.add(panelBas);
        panelRoot.setCellHeight(panelBas,"90%");
                    
        putCmdInListe(listeCommande, panelCentre);
        
        ScrollPanel scrollPan = new ScrollPanel(panelRoot);
        RootLayoutPanel.get().add(scrollPan);        
        History.addValueChangeHandler(this);
        History.fireCurrentHistoryState();
    }

    @Override
    public void onValueChange(ValueChangeEvent event) {
        String value = (String) event.getValue();
        if (value.contains("?")) {
            String parametre = value.substring(15);
            listeCommande.get("lister_seances").setCondition(parametre);
            listeCommande.get("lister_seances").execute();
        }else
        if (value.equals("Connexion")) {
            ihmConnexion.changeAuthentification();
        } else if (value.equals("Deconnexion")) {
            ihmConnexion.changeAuthentification();
        } else {
            listeCommande.get(value).execute();
        }
    }    
    
    public void putCmdInListe(HashMap<String,Command> listeCommandes, SimplePanel panel)
    {
        listeCommande.put("se_connecter", new Cmd_identifier_utilisateur(panel));
        listeCommande.put("s_inscrire", new Cmd_inscrire_uilisateur(panel));
        listeCommande.put("modifier_profil", new Cmd_modifier_profil(panel));
        listeCommande.put("gerer_compte", new Cmd_gerer_compte(panel));
        listeCommande.put("lister_seances", new Cmd_lister_seances(panel));
        listeCommande.put("creer_seance", new Cmd_creer_seance(panel));
    }
}
